package com.dxy.wx.controller;

import com.dxy.bean.Courier;
import com.dxy.bean.Message;
import com.dxy.bean.User;
import com.dxy.mvc.ResponseBody;
import com.dxy.service.CourieService;
import com.dxy.service.UserService;
import com.dxy.service.impl.CourieServiceImpl;
import com.dxy.service.impl.UserServiceImpl;
import com.dxy.utils.JSONUtil;
import com.dxy.utils.RandomUtil;
import com.dxy.utils.UserUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @author DELL
 */
public class UserController {
    @ResponseBody("/wx/loginSms.do")
    public String sendSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        // String code = RandomUtil.getCode()+"";
        // boolean flag = SMSUtil.loginSMS(userPhone, code);
       //  调用工具类随机生成雁阵码
        String code = RandomUtil.getCode()+"";
        boolean flag = true;
        Message msg = new Message();
        if(flag){
            //短信发送成功
            msg.setStatus(0);
            msg.setResult("您的验证码为:"+code);
        }else{
            //短信发送失败
            msg.setStatus(1);
            msg.setResult("验证码下发失败,请检查手机号或稍后再试");
        }
        UserUtil.setLoginSms(request.getSession(),userPhone,code);

        String json = JSONUtil.toJSON(msg);
        return json;
    }

    /***
     * 手机登录操作
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        UserService userService = new UserServiceImpl();
        CourieService courieService = new CourieServiceImpl();
        // 接收登录页面传过来的手机号
        String WxUserPhone = request.getParameter("userPhone");
        //  接收验证码
        String userCode = request.getParameter("code");

        /**
         * 判断登录用户信息
         */
        // 调用userService方法查询用户
        String uPhone = userService.findUserPhone(WxUserPhone);
        //调用courieService方法查询管理员
        String cPhone = courieService.findCouierPhone(WxUserPhone);
        String sysCode = UserUtil.getLoginSms(request.getSession(), WxUserPhone);
        System.out.println(sysCode);
        Message msg = new Message();
        if(sysCode == null){
            //这个手机号未获取短信
            msg.setStatus(-1);
            msg.setResult("手机号码未获取短信");
        }else if(sysCode.equals(userCode)){
            //这里手机号码和短信一致 , 登陆成功
            //TODO 这个判断应替换为快递员表格查询手机号的结果
            User user = new User();
            if(WxUserPhone.equals(cPhone)){
                //快递员
                msg.setStatus(1);
                user.setUser(false);
                user.setUserPhone(cPhone);
            }else if(WxUserPhone.equals(uPhone)){
                //用户
                msg.setStatus(0);
                user.setUser(true);
                user.setUserPhone(uPhone);
            }else {
                msg.setStatus(-1);

                msg.setResult("用户未注册跳转注册页面！");
            }
            UserUtil.setWxUser(request.getSession(),user);
            Courier courier=new Courier();
            courier.setUserPhone(user.getUserPhone());
            //没有调用setWxCourier方法，直接调用getWxCourier发方法是为null的
            UserUtil.setWxCourier(request.getSession(),courier);
        }else{
            //这里是验证码不一致 , 登陆失败
            msg.setStatus(-2);
            msg.setResult("验证码不一致,请检查");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response){
        User user = UserUtil.getWxUser(request.getSession());
        boolean isUser = user.isUser();
        String username = user.getUsername();

        Message msg = new Message();
        if(isUser) {
            msg.setStatus(0);
        } else {
            msg.setStatus(1);
        }
        msg.setResult(user.getUserPhone());
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    /***
     * 退出操作
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        //1.    销毁session
        request.getSession().invalidate();
        //2.    给客户端回复消息
        Message msg = new Message(0);
        return JSONUtil.toJSON(msg);
    }

    /**
     * 获取用户姓名
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/username.do")
    public String getName(HttpServletRequest request, HttpServletResponse response){
        UserService userService = new UserServiceImpl();
        CourieService courieService = new CourieServiceImpl();
        User user = UserUtil.getWxUser(request.getSession());
        Courier courier = UserUtil.getWxCourier(request.getSession());
        boolean isUser = user.isUser();
        String name=null;
        Message msg = new Message();
        // 获取当前用户手机号
        String userPhone = user.getUserPhone();
        if(isUser) {

            name = userService.findUserName(userPhone);
            msg.setStatus(0);
            msg.setResult(name);


        } else {
            msg.setStatus(1);
            String userPhone1 = courier.getUserPhone();
            name = courieService.findCouiername(userPhone1);
            msg.setResult(name);

        }
        msg.getResult();
        String json = JSONUtil.toJSON(msg);
        System.out.println(json);
        return json;

    }
}
